/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cs5631threadinglab;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author brady
 */
public class Environment {
    int rows;
    int cols;
    static LinkedList<Cell> cells;
    /**
     * Constructor for environment
     * @param rows number of rows in the environment
     * @param cols number of columns in the environment
     */
    public Environment(int rows, int cols)
    {
        cells = new LinkedList<Cell>();
        this.rows = rows;
        this.cols = cols;
        for(int i = 0; i<rows; i++)
        {
            for(int j = 0; j<cols; j++)
            {
                cells.add(new Cell(i, j, cols * i + j));
            }
        }
    }

 /**
  * Get the cell fromt the environment
  * @param r row of cell to get
  * @param c column of cell to get
  * @return the cell at row, col
  */
    public static Cell GetCell(int r, int c)
    {
        for (Cell cell : cells)
        {
            if(cell.row == r && cell.col == c)
            {
                return cell;
            }
        }
        return null;
    }


    /**
     * gets the particle from a cell
     * @param row the row of the cell
     * @param col the col of the cell
     * @return the particle in the cell if any, null otherwise
     */
    static synchronized Particle GetParticleFromCell(int row, int col)
    {

        Particle p = null;
        p = GetCell(row, col).ReleaseParticle();
        return p;
    }


    /**
     * places a particle in the cell specified
     * @param p the particle to place in the cell
     * @param row row of the cell
     * @param col col of the cell
     * @return true on success, false otherwise
     */
    static boolean GiveParticleToCell(Particle p, int row, int col)
    {
        return GetCell(row, col).AcceptParticle(p);
    }
}
